using System.Web.Mvc;
using Wikey.Domain.Components;
using Wikey.Domain.Repository;
using Wikey.Transformation;
using Factory=Wikey.Domain.Components.Factory;

namespace Wikey.Web.Mvc.Controllers
{
    public class ContentController : Controller
    {
        //
        // GET: /Content/

        [AcceptVerbs(HttpVerbs.Post),ValidateInput(false)]
        public ContentResult Index(int? id)
        {
            if (!id.HasValue)
                return null;

            var @data = Request.Params["data"];

            var repo = Factory.Get<IContentNodeRepository>();

            var contentNode = repo.Find(id.Value);
            contentNode.Text = data;
            repo.Save(contentNode);

            var result = new ContentResult
                             {
                                 Content = Transform.AsString(contentNode.Text)
                                 ,
                                 ContentType = "text/html"
                             };
            return result;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        [CompressFilter]
        public ContentResult Get(int? id )
        {

            var repo = Factory.Get<IContentNodeRepository>();

            var contentNode = repo.Find(id.Value);

            var result = new ContentResult
            {
                Content = contentNode.Text
                ,
                ContentType = "text/html"
            };
            return result; 
        }

        public ActionResult Delete (int? id )
        {
            if(!id.HasValue) return null;

            Factory.Get<IContentNodeRepository>().Delete(id.Value);
            return View();
        }

    }
}
